package com.khadi.testcase.view.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TestCaseTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3130880338830734279L;

	private String[] columns;
	private int rows;

	private List<TestCaseModel> data;

	public TestCaseTableModel(String[] columns, int rows) {
		this.columns = columns;
		this.rows = rows;
	}

	@Override
	public int getColumnCount() {
		return this.columns.length;
	}

	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return this.columns[column];
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return rows;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		TestCaseModel model = getValueAt(rowIndex);

		if (model != null) {
			String columnName = getColumnName(columnIndex);
			return model.getPropertyMap().get(columnName);

		}
		return null;
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		TestCaseModel model = getValueAt(rowIndex);
		if (model != null && value != null) {
			String columnName = getColumnName(columnIndex);
			model.setPropertyValue(columnName, String.valueOf(value));
		}
	}

	public void setData(List<TestCaseModel> data) {
		this.data = data;
		fireTableDataChanged();
	}

	public List<TestCaseModel> getData() {
		if (data == null) {
			data = new ArrayList<TestCaseModel>();
		}
		return data;
	}

	public TestCaseModel getValueAt(int rowIndex) {

		return getData().get(rowIndex);
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		return true;
	}
}
